package com.hanqian.kepler.web.controller.core;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hanqian.kepler.common.bean.jqgrid.JqGridFilter;
import com.hanqian.kepler.common.bean.jqgrid.JqGridPager;
import com.hanqian.kepler.common.bean.jqgrid.JqGridReturn;
import com.hanqian.kepler.common.bean.result.AjaxResult;
import com.hanqian.kepler.common.enums.BaseEnumManager;
import com.hanqian.kepler.common.jpa.specification.Rule;
import com.hanqian.kepler.common.jpa.specification.SpecificationFactory;
import com.hanqian.kepler.core.entity.primary.core.TargetDesc;
import com.hanqian.kepler.core.entity.primary.core.TargetDescZan;
import com.hanqian.kepler.core.service.core.TargetDescService;
import com.hanqian.kepler.core.service.core.TargetDescZanService;
import com.hanqian.kepler.core.service.core.TargetService;
import com.hanqian.kepler.flow.entity.User;
import com.hanqian.kepler.security.annotation.CurrentUser;
import com.hanqian.kepler.web.annotation.RequestJsonParam;
import com.hanqian.kepler.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指标描述 - 点赞记录
 * ============================================================================
 * author : dzw
 * createDate:  2021/1/12 。
 * ============================================================================
 */
@Controller
@RequestMapping("targetDescZan")
public class TargetDescZanController extends BaseController {

	@Autowired
	private TargetService targetService;
	@Autowired
	private TargetDescService targetDescService;
	@Autowired
	private TargetDescZanService targetDescZanService;

	/**
	 * 获取评论列表
	 */
	@RequestMapping("list")
	@ResponseBody
	public JqGridReturn list(JqGridPager pager, @RequestJsonParam("filters") JqGridFilter filters, String id, String v){
		List<Rule> rules = getJqGridSearch(filters);
		Pageable pageable = getJqGridPageable(pager);

		TargetDesc targetDesc = targetDescService.get(id);
		if(targetDesc != null){
			rules.add(Rule.eq("targetDesc.id", id));
			rules.add(Rule.eq("versionIndex", NumberUtil.isNumber(v) ? Convert.toInt(v) : targetDesc.getVersionIndex()));
		}

		Page<TargetDescZan> page = targetDescZanService.findAll(SpecificationFactory.where(rules), pageable);
		List<Map<String, Object>> dataRows = new ArrayList<>();
		page.getContent().forEach(entity -> {
			Map<String, Object> map = new HashMap<>();
			map.put("id", entity.getId());
			map.put("creator.name", entity.getCreator()!=null ? entity.getCreator().getName() : "未知");
			map.put("createTime", DateUtil.formatDateTime(entity.getCreateTime()));
			map.put("headerImg", entity.getCreator()!=null ? entity.getCreator().getAvatarId() : "");
			dataRows.add(map);
		});
		return getJqGridReturn(dataRows, page);
	}

	/**
	 * 删除点赞
	 */
	@PostMapping("delete/{id}")
	@ResponseBody
	public AjaxResult delete(@CurrentUser User user, @PathVariable String id){
		TargetDescZan targetDescZan = targetDescZanService.get(id);
		if(targetDescZan == null) return AjaxResult.error("找不到此");
		targetDescZan.setState(BaseEnumManager.StateEnum.Delete);
		targetDescZanService.save(targetDescZan);

		TargetDesc targetDesc = targetDescZan.getTargetDesc();
		if(targetDesc != null){
			targetDesc.setZanCount(targetDescZanService.getZanCount(targetDesc));
			targetDescService.save(targetDesc);
		}

		return AjaxResult.success();
	}

}
